<!-- pages/login/components/mobile.vue -->
<script setup lang="ts">
  import { getCodeApi, loginApi } from '@/apis/user'
  import { useUserStore } from '@/stores/user'
  import { ref } from 'vue'
  defineOptions({
    styleIsolation: 'shared',
  })

  // 是否显示倒计时
  const showCountDown = ref(false)

  //表单数据
  const formData = ref({
    mobile: '13230000094',
    code: '',
  })
  const isAgreement = ref(false)
  //表单校验
  const formRef = ref<UniHelper.UniFormsInstance>()
  const formRules: UniHelper.UniFormsRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请输入手机号' },
        { pattern: '^1[0-9]{10}$', errorMessage: '请输入正确的手机号' },
      ],
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: '^[0-9]{6}$', errorMessage: '请输入6位数字验证码' },
      ],
    },
  }

  // 提交表单
  const submitForm = async () => {
    try {
      const res = await formRef.value?.validate?.()
      console.log(res)
      if (!isAgreement.value) {
        uni.showToast({
          title: '请先同意用户协议和隐私协议',
          icon: 'none',
        })
        return
      }
      login()
    } catch (error) {
      console.log(error)
    }
  }

  // 获取验证码
  const getCode = async () => {
    if (!isAgreement.value) {
      uni.showToast({
        title: '请先同意用户协议和隐私协议',
        icon: 'none',
      })
      return
    }
    const res = await getCodeApi({ mobile: formData.value.mobile, type: 'login' })
    uni.showToast({
      title: '验证码已发送',
      icon: 'none',
    })
    showCountDown.value = true
    formData.value.code = res.code
  }

  // 登录
  const userStore = useUserStore()
  const login = async () => {
    try {
      const res = await loginApi({ mobile: formData.value.mobile, code: formData.value.code })
      uni.showToast({
        title: '登录成功',
        icon: 'success',
      })
      uni.switchTab({ url: '/pages/index/index' })
      userStore.setToken(res.token)
      userStore.userId = res.id
    } catch (error) {
      console.log(error)
    }
  }
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :rules="formRules" :model="formData">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        type="number"
        :maxlength="11"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        type="number"
        :maxlength="6"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        v-model="formData.code"
      />
      <text class="text-button" @click="getCode" v-if="!showCountDown"> 获取验证码 </text>
      <view class="text-button" v-else>
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="4"
          color="#16C2A3"
          @timeup="showCountDown = false"
        />
        <text> 秒后重新获取 </text>
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgreement" color="#16C2A3" @click="isAgreement = !isAgreement" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="submitForm">登 录</button>
  </uni-forms>
</template>

<style lang="scss">
  @import './styles.scss';
</style>
